package org.go.smart.log;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class GSLogger {
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
	private Logger javaLogger;
	private String logFile = "gosmart_" + sdf.format(new Date()) + ".log";
	private static GSLogger instance ;
	
	public static GSLogger getInstance() {
		if (instance == null) {
			instance = new GSLogger();
		}
		return instance;
	}
	
	private  GSLogger() {
		javaLogger = Logger.getLogger("GSLogger");  
	    FileHandler fh;  

	    try {  

	        fh = new FileHandler(logFile);  
	        javaLogger.addHandler(fh);
	        SimpleFormatter formatter = new SimpleFormatter();  
	        fh.setFormatter(formatter);  

	    } catch (SecurityException e) {  
	        e.printStackTrace();  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    }  
	}
	
	public void info(final String message) {
		javaLogger.info(message);
	}
	
	public void warning(final String message) {
		javaLogger.warning(message);
	}
	
	public void severe(final String message) {
		javaLogger.severe(message);
	}

	public static String formatDate(Date date) {
		return sdf.format(date);
	}
	
	public static String readFile(String path, Charset encoding) throws IOException {
		byte[] encoded = Files.readAllBytes(Paths.get(path));
		return encoding.decode(ByteBuffer.wrap(encoded)).toString();
	}

	

}
